package version1

import (
	"server/global"
	operatemodel "server/model/operate"
	"server/myutils"

	"github.com/gin-gonic/gin"
)

//@Summary 获取所有分类
//@Router /v1/getcategories  [get]
//@Tags Gets
//@Description 获取所有的分类信息
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func GetAllcategory(req *gin.Context) {
	c := operatemodel.Getallcate()
	myutils.ResultJSON(req, global.SUCCESS, c)
}

func AddCategory(req *gin.Context) {
	type bin struct {
		Categoryname string `json:"categoryname" binding:"required"`
	}
	var bind = new(bin)
	if req.ShouldBindJSON(bind) != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
	}
	var code = operatemodel.Addcate(bind.Categoryname)
	myutils.ResultJSON(req, code, nil)
}

func EditCategory(req *gin.Context) {
	type bin struct {
		Oldname string `json:"oldname" binding:"required"`
		Newname string `json:"newname" binding:"required"`
	}
	var bind = new(bin)
	if req.ShouldBindJSON(bind) != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
		return
	}
	myutils.ResultJSON(req, operatemodel.Editcate(bind.Oldname, bind.Newname), nil)
}

func DeleteCategory(req *gin.Context) {

	type bin struct {
		//使用uri读取信息要设定tag
		Categoryname string `json:"categoryname" binding:"required"`
		ID           uint   ` binding:"required"`
	}
	var bind = new(bin)
	if req.ShouldBindJSON(bind) != nil {
		myutils.ResultJSON(req, global.ERROR_FORMAT, nil)
		return
	}
	myutils.ResultJSON(req, operatemodel.Deletecate(bind.Categoryname, bind.ID), nil)
}
